<?php defined('LFNC_PACKAGE') or die('forbidden');


/**
* MLT Web Engine
* http://www.molotsoft.ru
*
* @package MLT_WE
* @subpackage Lucky_Funcs
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: pgsql.lfnc.php 259 2009-06-19 05:12:52Z molotsoft@gmail.com $
* @link $HeadURL: http://lucky-funcs.googlecode.com/svn/branches/version_1/funcs/pgsql.lfnc.php $
*/


function is_pg_link($db) {
	return is_resource_type($db, 'pgsql link') || is_resource_type($db, 'pgsql link persistent');
}

function is_pg_result($dbr) {
	return is_resource_type($dbr, 'pgsql result');
}


function pg_xconnect($connection_string) {
	$db = pg_connect($connection_string);
	if (is_pg_link($db)) register_shutdown_function('pg_close', $db);
	return $db;
}


function pg_escape($string) {
	return pg_escape_string($string);
}

function pg_escape_deep($value) {
	return is_array($value) ? array_map('pg_escape_deep', $value) : pg_escape($value);
}

function vsprintf_pg_escape($format, $args) {
	return vsprintf($format, pg_escape_deep($args));
}

function sprintf_pg_escape($format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return vsprintf_pg_escape($format, array_shifted($args));
}


function pg_vqueryf($db, $query_format, $args) {
	return pg_query($db, vsprintf_pg_escape($query_format, $args));
}

function pg_queryf($db, $query_format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return pg_vqueryf($db, $query_format, array_shifted($args, 2));
}


function pg_vexecf($db, $query_format, $args) {
	if (is_pg_result($dbr = pg_vqueryf($db, $query_format, $args))) {
		$affected_rows = pg_affected_rows($dbr);
		pg_free_result($dbr);
		return $affected_rows;
	}
	return $dbr;
}

function pg_execf($db, $query_format /* [, $args [, $... ]] */ ) {
	$args = func_get_args();
	return pg_vexecf($db, $query_format, array_shifted($args, 2));
}


function pg_fetch_value($dbr, $value_col = 0) {
	$row = pg_fetch_array($dbr, NULL, is_wholenum($value_col) ? PGSQL_NUM : PGSQL_ASSOC);
	return isset($row[$value_col]) ? $row[$value_col] : FALSE;
}


function pg_fetch_rows($dbr, $result_type = PGSQL_ASSOC) {
	if (pg_num_rows($dbr) >= 1) pg_result_seek($dbr, 0);
	$rows = array();
	while ($row = pg_fetch_array($dbr, NULL, $result_type)) $rows[] = $row;
	return $rows;
}

function pg_fetch_values($dbr, $value_col = 0) {
	if (pg_num_rows($dbr) >= 1) pg_result_seek($dbr, 0);
	$values = array();
	while (!is_false($value = pg_fetch_value($dbr, $value_col))) $values[] = $value;
	return $values;
}


function pg_fetch_rows_combine($dbr, $key_col = 'id', $result_type = PGSQL_ASSOC) {
	$rows = pg_fetch_rows($dbr, $result_type);
	$combine = array();
	foreach (pg_fetch_values($dbr, $key_col) as $i => $key) $combine[$key] = $rows[$i];
	return $combine;
}

function pg_fetch_values_combine($dbr, $key_col = 'id', $value_col = 'value') {
	$values = pg_fetch_values($dbr, $value_col);
	$combine = array();
	foreach (pg_fetch_values($dbr, $key_col) as $i => $key) $combine[$key] = $values[$i];
	return $combine;
}


?>